package com.example.proyyj.Controller;

import com.example.proyyj.Entity.Building;
import com.example.proyyj.Entity.Exhibit;
import com.example.proyyj.Entity.ResultEntity;
import com.example.proyyj.Service.ExhibitService;
import com.example.proyyj.Util.JSONTool;
import com.example.proyyj.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Exhibit/")//请求路径
public class ExhibitController {
    @Resource
    ExhibitService exhibitService;
    ResultEntity resultEntity=null;

    //添加展会
    @PutMapping("addExhibit")
    private String addExhibit (@RequestParam("Name")String Name,@RequestParam("Address") String Address ,@RequestParam("Date")String Date,@RequestParam("Information")String Information){
        resultEntity=new ResultEntity();
        try {
            if (exhibitService.addExhibit(Name, Address,Date,Information) > 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    //删除展会
    @DeleteMapping("delExhibit")
    private String delExhibitById (@RequestParam("id") int id){
        resultEntity =new ResultEntity();
        try {
            if (exhibitService.delExhibitById(id)>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功");
            }else {
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误联系管理员");
        }
        return  JsonUtil.getJsonTool().toJson(resultEntity);
    }

    //更改展会信息
    @PutMapping("upExhibit")
    private String upExhibit(@RequestParam("Name")String Name,@RequestParam("Address") String Address ,@RequestParam("Date")String Date,@RequestParam("Information")String Information,@RequestParam("id") int id){
        resultEntity=new ResultEntity();
        try{

            if(exhibitService.upExhibit(Name,Address,Date,Information,id) > 0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功");
            }else {
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    //按id查询展会
    @GetMapping("getExhibit")
    private String getExhibitById(@RequestParam("id")int id){
        resultEntity=new ResultEntity();
        try{
            Exhibit exhibit=exhibitService.getExhibitById(id);
            if(exhibit==null){
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此人");
            }else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(exhibit);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 查询所有展会
     * @param
     * @return
     */
    @GetMapping("getExhibitList")
    private  String exhibit(){

        try {
            resultEntity=new ResultEntity();
            List<Exhibit> list=exhibitService.getExhibitList();
            if (list!=null){
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setCount(list.size());
                resultEntity.setData(list);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("99999");
            resultEntity.setMsg("查询失败"+e.getMessage());
        }
        return  JSONTool.getJsonTool().toJson(resultEntity);
    }

    @GetMapping("RandomExhibit")
private  String randomExhibit(){
        resultEntity=new ResultEntity();
        try {
            List<Exhibit> list = exhibitService.randomExhibit();
            if (list !=null){
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setCount(list.size());
                resultEntity.setData(list);
            }else {
                resultEntity.setCode("1");
                resultEntity.setMsg("查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return  JSONTool.getJsonTool().toJson(resultEntity);
    }


}
